package com.person.mengshuai.mytestdemo.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.person.mengshuai.mytestdemo.domain.response.Result;
import com.person.mengshuai.mytestdemo.entity.BRole;
import com.person.mengshuai.mytestdemo.entity.BUser;
import com.person.mengshuai.mytestdemo.exception.base.BusinessException;
import com.person.mengshuai.mytestdemo.mapper.BUserMapper;
import com.person.mengshuai.mytestdemo.satoken.SaTokenConstant;
import com.person.mengshuai.mytestdemo.satoken.UserInfo;
import com.person.mengshuai.mytestdemo.service.BMenuService;
import com.person.mengshuai.mytestdemo.service.BRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 登录  前端控制器
 * </p>
 *
 * @author ms
 * @since 2024-08-02
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private BUserMapper userMapper;

    @Autowired
    private BRoleService bRoleService;

    @Autowired
    private BMenuService bMenuService;


    @GetMapping("/authLogin")
    public Result authLogin(@RequestParam String username, @RequestParam String password) throws BusinessException {
        BUser bUser = userMapper.findUserByUsernameAndPassword(username, SaSecureUtil.aesEncrypt("password", password));
        if (bUser == null) {
            throw new BusinessException("用户名或密码错误，请重新输入");
        }
        UserInfo userInfo = new UserInfo();
        String userId = bUser.getId();
        BeanUtils.copyProperties(bUser, userInfo);
        List<BRole> bRoleList = bRoleService.getBRoleListByUserId(userId);
        if (!CollectionUtils.isEmpty(bRoleList)) {
            userInfo.setRoleList(bRoleList.stream().map(BRole::getName).collect(Collectors.toList()));
            List<Integer> roleIds = bRoleList.stream().map(BRole::getId).collect(Collectors.toList());
            List<String> permissionNames = bMenuService.getPermissionNamesByRoleIds(roleIds);
            if (!CollectionUtils.isEmpty(permissionNames)) {
                userInfo.setPermissionList(permissionNames);
            }
        }
        StpUtil.login(userId);
        StpUtil.getSession().set(SaTokenConstant.USER_INFO, JSON.toJSONString(userInfo));
        return Result.success("登录成功");
    }

    /**
     * 获取当前登录用户信息
     *
     * @return 获取当前登录用户信息
     */
    @SaCheckLogin
    @GetMapping("/getCurrentUserInfo")
    public Result getCurrentUserInfo() {
        Object o = StpUtil.getSession().get(SaTokenConstant.USER_INFO);
        UserInfo userInfo = JSON.parseObject(o.toString(), UserInfo.class);
        return Result.success(userInfo);
    }
}
